package org.ahuanlien.crm.util;

import com.alibaba.fastjson.JSON;
import org.ahuanlien.crm.domain.SystemLog;
import org.ahuanlien.crm.service.ISystemLogService;
import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

public class SystemLogUtil {

	@Autowired
	private ISystemLogService systemLogService;


	public void writeLog(JoinPoint joinPoint) {
		if (joinPoint.getTarget() instanceof ISystemLogService) {
			return;
		}

		SystemLog systemLog = new SystemLog();
		//操作时间
		systemLog.setOpTime(new Date());
		//操作人
		systemLog.setOpUser(UserContext.getCurrentUser());
		//remoteAddr 远程的地址
		//操作的IP地址
		systemLog.setOpIP(UserContext.getRequest().getRemoteAddr());
		//操作的方法的参数
		systemLog.setParams(JSON.toJSONString(joinPoint.getArgs()));
		//操作的方法
		String className = joinPoint.getTarget().getClass().getName();
		String methodName = joinPoint.getSignature().getName();
		systemLog.setFunction(className + ":" + methodName);
		systemLogService.insert(systemLog);
	}
}
